<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript" src="js/jquery-3.7.0.min.js"></script>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/footer.css">
    <title>注册</title>
</head>
<body>
<div class="login-container">
    <h2>欢迎注册知识管理系统</h2>
    <form>
        <label for="username">用户名:</label>
        <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        <div id="tip_uname">&nbsp</div>
        <label for="password1">密码:</label>
        <input type="password" id="password1" name="password1" placeholder="请输入密码" required>
        <div id="tip_pwd1">&nbsp</div>
        <label for="password1">确认密码:</label>
        <input type="password" id="password2" name="password2" placeholder="请再次输入密码" required>
        <div id="tip_pwd2">&nbsp</div>
        <button type="button" id="submit">注册</button>
        <div id="tip_submit">&nbsp</div>
    </form>
    <a href="login.html">已有账号？去登录</a>
</div>
<div class="footer">
    软件21校企本1班 © 202124040126@王勤磊
</div>
<script>
    function httpPost(URL, PARAMS) {
        let temp = document.createElement("form");
        temp.action = URL;
        temp.method = "post";
        temp.style.display = "none";

        for (let x in PARAMS) {
            let opt = document.createElement("textarea");
            opt.name = x;
            opt.value = PARAMS[x];
            temp.appendChild(opt);
        }

        document.body.appendChild(temp);
        temp.submit();

        return temp;
    }

    $().ready(function (){
        $("#username").blur(function (){
            let xhr1 = new XMLHttpRequest();
            xhr1.onreadystatechange = function (){
                if(this.readyState===4){
                    if (this.status===200) {
                        $("#tip_uname").html(this.responseText)
                    }
                }
            }
            // xhr1.open("GET","checkUsernameRegister?username="+$("#username").val(),false);
            // xhr1.send();
            xhr1.open("POST","checkUsernameRegister",false)
            xhr1.setRequestHeader("content-type","application/x-www-form-urlencoded")
            xhr1.send("username="+$("#username").val())
        })
        $("#password1").blur(function (){
            let xhr2 = new XMLHttpRequest();
            xhr2.onreadystatechange = function (){
                if(this.readyState===4){
                    if (this.status===200) {
                        $("#tip_pwd1").html(this.responseText)
                    }
                }
            }
            xhr2.open("POST","checkPassword1Register",false);
            xhr2.setRequestHeader("content-type","application/x-www-form-urlencoded")
            xhr2.send("password1="+$("#password1").val());
        })
        $("#password2").blur(function (){
            let xhr3 = new XMLHttpRequest();
            xhr3.onreadystatechange = function (){
                if(this.readyState===4){
                    if (this.status===200) {
                        $("#tip_pwd2").html(this.responseText)
                    }}}
            xhr3.open("POST","checkPassword2Register",false);
            xhr3.setRequestHeader("content-type","application/x-www-form-urlencoded")
            xhr3.send("password1="+$("#password1").val()+"&password2="+$("#password2").val());
        })
        $("#submit").click(function () {
            $.ajax({
                type: "POST",
                url: "register",
                datatype: "text",
                data: "username=" + $("#username").val() + "&password1=" + $("#password1").val() + "&password2=" + $("#password2").val() + "&date=" + new Date().getTime(),
                success: function (dataStr) {
                    let dataJson = JSON.parse(dataStr)
                    alert(dataJson.msg)
                    if (dataJson.code == 0) {
                        let params = {
                            username: dataJson.obj.username,
                            password: dataJson.obj.password
                        }
                        httpPost("index.html", params);
                    } else {
                        $("#tip_submit").html("<font color=red>" + dataJson.msg + "</font>")
                    }}})})
        document.onkeydown= function (e) {
            let theEvent = window.event || e;
            let code = theEvent.keyCode || theEvent.which;
            if (code == 13) {
                $("#submit").click();
            }
        }
    })
</script>
</body>
</html>